/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.date;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.DateMapper;
import main_com.pojo.LoginBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/AddDateController")
public class AddDateController {
    //     public void wrong1(){
//        String msg="请把日期填写完整，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong2(){
//        String msg="请确认日期填写正确，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong3(){
//        String msg="请填写日程内容，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void wrong4(){
//        String msg="该日程已有计划，添加失败！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
//    public void right(){
//        String msg="填写信息合格，添加成功！";
//        int type=JOptionPane.YES_NO_CANCEL_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, type);
//    }
    @Autowired
    DateMapper dateMapper;

    @RequestMapping("/addDate")
    protected void addDate(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String day = request.getParameter("day");
        String thing = request.getParameter("thing");
        if (year.isEmpty())
            year = "22";
        if (month.isEmpty())
            month = "01";
        if (day.isEmpty())
            day = "01";
        year = String.valueOf(Integer.parseInt(year));
        month = String.valueOf(Integer.parseInt(month));
        day = String.valueOf(Integer.parseInt(day));
        String date = "20" + year + "-" + month + "-" + day;
//        if (year.isEmpty() || month.isEmpty() || day.isEmpty()) {
////            wrong1();
//            response.sendRedirect("http://localhost/dateManager/addDate.jsp");
//        } else
        if (year.length() != 2 || Integer.parseInt(year) < 11 || Integer.parseInt(month) < 1 || Integer.parseInt(month) > 12 || Integer.parseInt(day) < 1 || Integer.parseInt(day) > 31) {
//            wrong2();
            response.sendRedirect("http://localhost/dateManager/addDate.jsp");
        } else if (thing.isEmpty()) {
//            wrong3();
            response.sendRedirect("http://localhost/dateManager/addDate.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.isEmpty()) {
                    response.sendRedirect("http://localhost/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                int k = dateMapper.findDateByDateAndUserName(date, userName);
                if (k > 0) {
//                    wrong4();
                    response.sendRedirect("http://localhost/dateManager/addDate.jsp");
                    return;
                } else {
                    dateMapper.insertDate(userName, date, thing);

                    // 获取当前分页相关参数（假设每页显示数量固定为5条，这里可以根据实际情况调整）
                    int pageSize = 5;
                    String pageParam = request.getParameter("page");
                    int currentPage = (pageParam != null) ? Integer.parseInt(pageParam) : 1;
                    int offset = (currentPage - 1) * pageSize;

                    // 根据分页参数获取当前页的日程数据
                    ArrayList datelist = dateMapper.getDateByUserNameWithPagination(userName, offset, pageSize);
                    session.setAttribute("datelist", datelist);

                    // 将当前页码和总页数也存入会话（计算总页数逻辑与之前在LookDateController中类似，这里简化处理，假设已有获取总记录数的方法）
                    int totalCount = dateMapper.countDateByUserName(userName);
                    int totalPages = (int) Math.ceil((double) totalCount / pageSize);
                    session.setAttribute("currentPage", currentPage);
                    session.setAttribute("totalPages", totalPages);
                }
//                right();
                response.sendRedirect("http://localhost/dateManager/lookDate.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}